package com.frameDemo.app;

import com.frameDemo.frame.Frame;
import com.frameDemo.frame.FrameData;
import com.frameDemo.frame.FrameHead;
import com.frameDemo.socket.SokectDemoClient;

import java.io.IOException;

/**
 * Created by TianyuanPan on 10/16/16.
 */
public class FrameClentApp {


    private static SokectDemoClient client;

    private static Frame xueyangFrame;
    private static Frame xuetangFrame;

    static {

        xueyangFrame = Frame.initFrame(FrameHead.initFrameHead(new byte[]{
                0x55, (byte)0xAA, 0x01, (byte)0x80, 0x00, 0x00, 0x00, 0x00, 0x44, 0x00, (byte) 0xEA, 0x52
        }), FrameData.initFrameData(new byte[]{0x31, 0x30, 0x33, 0x31, 0x30, 0x33, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30,
                0x30, 0x31, 0x39, 0x33, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x38,
                0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, (byte) 0xDF, 0x07, 0x0B,
                0x06, 0x0E, 0x1D, 0x31, 0x00, 0x62, (byte) 0xCC, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x3A, 0x00
        }));



        xuetangFrame = Frame.initFrame(FrameHead.initFrameHead(new byte[]{
                0x55, (byte) 0xAA, 0x01, (byte) 0x82, 0x00, 0x00, 0x00, 0x00, 0x44, 0x00, (byte) 0xA8, (byte) 0x92
        }), FrameData.initFrameData(new byte[]{
                0x31, 0x30, 0x33, 0x31, 0x30, 0x33, 0x30, 0x31, 0x30, 0x30,
                0x31, 0x30, 0x30, 0x30, 0x31, 0x39, 0x33, 0x78, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x38, 0x38, 0x36, 0x36, 0x00, 0x00,
                0x00, 0x00, 0x07, 0x00, 0x00, 0x00, (byte) 0xDF, 0x07, 0x0B, 0x06,
                0x0E, 0x19, 0x10, 0x00, (byte) 0xCD, (byte) 0xCC, (byte) 0x8C, 0x3F, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x3A, 0x00
        }));
    }

    private String host;
    private int port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    private static void usage() {

        System.out.println("操作提示：");
        System.out.println("\t -h <server host> -p <server port>");
    }

    public static void main(String[] args) {

        if (args.length < 4 ) {
            usage();
            System.exit(0);
        }

        if (!args[0].equals("-h") || !args[2].equals("-p")) {
            usage();
            System.exit(0);
        }

        String host = args[1];

        int port = -1;

        try {
            port = Integer.parseInt(args[3]);
        } catch (NumberFormatException e) {
            usage();
            System.exit(0);
        }

        if (!(0 < port && port < 65536)) {
            usage();
            System.exit(0);
        }

        FrameClentApp app = new FrameClentApp();

        app.setHost(host);
        app.setPort(port);

        String serverReturn = "";


        while (true) {
            System.out.println("\n=======> 输入 1： 发送 血氧 数据");
            System.out.println("=======> 输入 2： 发送 血糖 数据");
            System.out.println("=======> 输入 q： 退出程序:");
            try {
                char c = (char) System.in.read();

                switch (c){
                    case '1':
                         client = SokectDemoClient.getClientSocket(host, port);
                        serverReturn = client.sendFrame(xueyangFrame.getFrameBytes());
                        System.out.println("服务器回复：\n\t\t" + serverReturn + "\n\n");
                        break;
                    case '2':
                        client = SokectDemoClient.getClientSocket(host, port);
                        serverReturn = client.sendFrame(xuetangFrame.getFrameBytes());
                        System.out.println("服务器回复：\n\t\t" + serverReturn + "\n\n");
                        break;
                    case 'q':
                    case 'Q':
                        System.exit(0);
                        break;
                    default:
                        break;
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}






